<template>
  <el-dialog
    title="新增员工"
    :visible="showDialog"
    width="40%"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="姓名" label-width="100px" prop="username">
        <el-input v-model="form.username" placeholder="请输入员工姓名" />
      </el-form-item>
      <el-form-item label="手机" label-width="100px" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" label-width="100px" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="转正时间" label-width="100px">
        <el-date-picker v-model="form.correctionTime" placeholder="请选择转正时间" />
      </el-form-item>
      <el-form-item label="聘用形式" label-width="100px">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option
            v-for="item in hireType.slice(0,2)"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" label-width="100px" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" label-width="100px" prop="departmentName">
        <el-cascader
          v-model="form.departmentName"
          :options="departmentList"
          :props="defaultProps"
          placeholder="请选择部门"
          @focus="getDepartmentList"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="add">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import EmployeeEnum from '@/api/constant/employees'
import { getDepartmentList, listToTree } from '@/api/departments'
import { addEmployee } from '@/api/employees'
export default {
  name: 'AddEmployee',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        username: '', // 姓名
        mobile: '', // 手机号
        formOfEmployment: '', // 聘用形式
        workNumber: '', // 工号
        departmentName: '', // 部门
        timeOfEntry: '', // 入职时间
        correctionTime: '' // 转正时间
      },
      rules: {
        username: [
          { required: true, message: '员工姓名不能为空' },
          { pattern: /^[\u4e00-\u9fa5]{2,4}$/, message: '用户名为2-4位' }
        ],
        mobile: [
          { required: true, message: '员工手机号不能为空' },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空' }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间' }
        ]
      },
      defaultProps: {
        label: 'name',
        value: 'name',
        expandTrigger: 'hover'
      },
      hireType: EmployeeEnum.hireType, // 聘用形式
      departmentList: [] // 部门列表
    }
  },
  methods: {
    // 关闭dialog弹窗
    close() {
      this.$emit('update:showDialog', false)
      // 重置表单校验规则和数据
      this.$refs.form.resetFields()
    },
    // 选择部门触发的获取部门列表数据
    async getDepartmentList() {
      const { depts } = await getDepartmentList()
      // 获取到的部门列表数据转换为树状数据
      this.departmentList = listToTree(depts)
      // console.log(this.departmentList)
    },
    async add() {
      try {
        await this.$refs.form.validate()
      } catch {
        return
      }
      await addEmployee({
        ...this.form,
        departmentName: this.form.departmentName.slice(-1)[0]
        // 请求传参数据不能是一个数组要用字符串
        // departmentName: this.form.departmentName[this.form.departmentName.length - 1]
      })
      this.$emit('update:showDialog', false)
      this.$message.success('添加员工成功')
      // 调用父组件的方法重新渲染
      this.$parent.getEmployeeList()
    }
  }
}
</script>

<style>

</style>
